﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using Abp.UI;
using Camc.OutsourceProcession.Organization;
using JetBrains.Annotations;

namespace Camc.OutsourceProcession.Order
{
	public class ProductionOrder:FullAuditedEntity<long>,IMayHaveCompanyCode
	{
		public ProductionOrder()
		{
			ProcessionState = ProductionProcessionState.发布;
            State = ProductionState.正常;
        }

		public string TaskCodeForOutside { set; get; }
		public string ProductionDrawingCodeForOutside { set; get; }
		public string ProductionSpecialForOutside { set; get; }
		public string TaskModelForOutside { set; get; }
		public string TaskKey { set; get; }
		public ICollection<ExecuteHistory> Historys { set; get; }
		public ProductionState State { internal set; get; }
		public ProductionProcessionState ProcessionState { internal set; get; }
		public int Quantity { set; get; }
		public DateTime DeliveryTime { set; get; }
		public string CompanyCode { get; set; }

		/// <summary>
		/// 暂停
		/// </summary>
		public void SetSuspend() => State = ProductionState.暂停;

		/// <summary>
		/// 正常
		/// </summary>
		public void SetNormal() => State = ProductionState.正常;

		/// <summary>
		/// 作废
		/// </summary>
		public void SetDispose() => State = ProductionState.作废;
		/// <summary>
		/// 报废
		/// </summary>
		public void SetScrap() => State = ProductionState.报废;


		/// <summary>
		/// 取回任务（从制造商手中取回任务）
		/// </summary>
		public void SetRenew(string companyCode)
		{
			ProcessionState = ProductionProcessionState.发布;
			CompanyCode = companyCode;
		}
		/// <summary>
		/// 接收
		/// </summary>
		public void SetReceive(string companyCode)
		{
			if (ProcessionState == ProductionProcessionState.发布)
			{
				ProcessionState = ProductionProcessionState.接收;
				CompanyCode = companyCode;
			}
			else
			{
				throw new UserFriendlyException("该任务处于[{0}]状态，不能接收!", ProcessionState.ToString());
			}
		}

		/// <summary>
		/// 加工
		/// </summary>
		public void SetProcess()
		{
			if ((ProcessionState == ProductionProcessionState.接收 || ProcessionState == ProductionProcessionState.加工) && State == ProductionState.正常)
			{
				ProcessionState = ProductionProcessionState.加工;
			}
			else
			{
				throw new UserFriendlyException("该任务处于[{0}]状态，不能接收!", ProcessionState.ToString());
			}
		}

		/// <summary>
		/// 交付
		/// </summary>
		public void SetDeliver()
		{
			if (ProcessionState == ProductionProcessionState.加工 && State == ProductionState.正常)
			{
				ProcessionState = ProductionProcessionState.交付;
			}
			else
			{
				throw new UserFriendlyException("该任务处于[{0}]状态，不能接收!", ProcessionState.ToString());
			}
		}
	}
}
